import 'package:android_location_mocker_example/toast_util.dart';
import 'package:android_location_mocker_example/widget/custom_textfield_widget.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'logic.dart';

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<HomeLogic>();
    final state = Get.find<HomeLogic>().state;
    return Scaffold(
        appBar: AppBar(title: const Text('模拟定位')),
        body: Column(children: [
          Expanded(
              child: SingleChildScrollView(
                  child: Column(children: [
            Container(
                margin:
                    const EdgeInsets.symmetric(horizontal: 15, vertical: 20),
                padding:
                    const EdgeInsets.symmetric(horizontal: 15, vertical: 20),
                decoration: BoxDecoration(color: Colors.white, boxShadow: [
                  BoxShadow(
                      color: Colors.grey.shade300,
                      spreadRadius: 5,
                      blurRadius: 5)
                ]),
                child: Center(
                    child: Column(children: [
                  const Padding(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Text("判断状态",
                          style: TextStyle(fontWeight: FontWeight.bold))),
                  ElevatedButton(
                      onPressed: () async {
                        bool res =
                            await logic.androidLocationMockerPlugin.isReady();
                        showToast('$res');
                      },
                      child: const Text("是否设置了模拟app")),
                  ElevatedButton(
                      onPressed: () async {
                        var res = await logic.androidLocationMockerPlugin
                            .isOpenDevMode();
                        showToast('$res');
                      },
                      child: const Text("是否开启开发者模式")),
                  ElevatedButton(
                      onPressed: () async {
                        var res = await logic.androidLocationMockerPlugin
                            .openDevMode();
                        showToast('$res');
                      },
                      child: const Text("打开开发者模式"))
                ]))),
            Container(
                margin: const EdgeInsets.symmetric(horizontal: 15),
                decoration: BoxDecoration(color: Colors.white, boxShadow: [
                  BoxShadow(
                      color: Colors.grey.shade300,
                      spreadRadius: 5,
                      blurRadius: 5)
                ]),
                padding: const EdgeInsets.all(10),
                child: Column(children: [
                  const Padding(
                      padding: EdgeInsets.symmetric(vertical: 10),
                      child: Text("配置",
                          style: TextStyle(fontWeight: FontWeight.bold))),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text("启用GPS"),
                        Obx(() {
                          return Switch(
                              value: state.isUseGps.value,
                              onChanged: (a) {
                                state.isUseGps.value = a;
                              });
                        })
                      ]),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        const Text("启用Network定位"),
                        Obx(() {
                          return Switch(
                              value: state.isUseNetwork.value,
                              onChanged: (a) {
                                state.isUseNetwork.value = a;
                              });
                        })
                      ]),
                  Container(
                      decoration:
                          BoxDecoration(border: Border.all(color: Colors.grey)),
                      child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            const Padding(
                                padding: EdgeInsets.all(8.0),
                                child: Text("输入qng高德API",
                                    style: TextStyle(
                                        fontSize: 12, color: Colors.grey))),
                            Obx(() {
                              return CustomTextField(
                                  value: state.key.value,
                                  onChanged: (t) {
                                    state.key.value = t;
                                  },
                                  inputDecoration: const InputDecoration(
                                      border: InputBorder.none,
                                      contentPadding:
                                          EdgeInsets.symmetric(horizontal: 10),
                                      isCollapsed: true,
                                      isDense: true));
                            }),
                            const Padding(
                                padding: EdgeInsets.all(8.0),
                                child: Text("免费用户每天100次,若发现搜索不了，可自行申请",
                                    style: TextStyle(
                                        fontSize: 12, color: Colors.grey)))
                          ])),
                  Container(
                      margin: const EdgeInsets.symmetric(vertical: 20),
                      decoration:
                          BoxDecoration(border: Border.all(color: Colors.grey)),
                      child: Column(children: [
                        const Padding(
                            padding: EdgeInsets.all(8.0),
                            child: Text("请输入定位频率(毫秒)",
                                style: TextStyle(
                                    fontSize: 12, color: Colors.grey))),
                        Obx(() {
                          return CustomTextField(
                              value: state.frequencyController.value,
                              onChanged: (t) {
                                state.frequencyController.value = t;
                              },
                              textAlign: TextAlign.center,
                              inputDecoration: const InputDecoration(
                                  border: InputBorder.none,
                                  contentPadding:
                                      EdgeInsets.symmetric(horizontal: 10),
                                  isCollapsed: true,
                                  isDense: true));
                        })
                      ])),
                  Row(children: [
                    Expanded(
                        child: Container(
                            padding: const EdgeInsets.all(5),
                            decoration: BoxDecoration(
                                border: Border.all(color: Colors.grey),
                                borderRadius: BorderRadius.circular(5)),
                            child: Obx(() {
                              return CustomTextField(
                                  maxLines: null,
                                  style: const TextStyle(fontSize: 13),
                                  inputDecoration: const InputDecoration(
                                      hintStyle: TextStyle(fontSize: 13),
                                      isCollapsed: true,
                                      isDense: true,
                                      border: InputBorder.none),
                                  onChanged: (t) {
                                    state.address.value = t;
                                  },
                                  value: state.address.value);
                            }))),
                    const SizedBox(width: 15),
                    InkWell(
                        onTap: logic.search,
                        child: Container(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 20, vertical: 10),
                            decoration: BoxDecoration(
                                color: Colors.blue,
                                borderRadius: BorderRadius.circular(5)),
                            child: const Text("搜索",
                                style: TextStyle(color: Colors.white))))
                  ]),
                  Obx(() {
                    if (state.list.isEmpty) {
                      return const Center(child: Text("请搜索地址"));
                    }
                    return ExpansionTile(
                        title: const Text("地址列表(点选)"),
                        children: [
                          Obx(() {
                            return ListView.builder(
                                shrinkWrap: true,
                                physics: const NeverScrollableScrollPhysics(),
                                itemCount: state.list.length,
                                itemBuilder: (BuildContext context, int index) {
                                  var bean = state.list[index];
                                  return InkWell(
                                      onTap: () {
                                        logic.select(bean);
                                      },
                                      child: Container(
                                          padding: const EdgeInsets.all(10),
                                          margin: const EdgeInsets.symmetric(
                                              horizontal: 5, vertical: 5),
                                          decoration: BoxDecoration(
                                              color: Colors.grey.shade200),
                                          child: Column(
                                              crossAxisAlignment:
                                                  CrossAxisAlignment.start,
                                              children: [
                                                Text("${bean.address}",
                                                    style: const TextStyle(
                                                        fontWeight:
                                                            FontWeight.bold)),
                                                Text(
                                                    "location:${bean.location}")
                                              ])));
                                });
                          })
                        ]);
                  }),
                  Container(
                      margin: const EdgeInsets.symmetric(vertical: 20),
                      decoration:
                          BoxDecoration(border: Border.all(color: Colors.grey)),
                      child: Column(children: [
                        const Padding(
                            padding: EdgeInsets.all(8.0),
                            child: Text("可手动修改经纬度:116.3452,39.8234",
                                style: TextStyle(
                                    fontSize: 12, color: Colors.grey))),
                        Obx(() {
                          return CustomTextField(
                              value: state.location.value,
                              onChanged: (t) {
                                state.location.value = t;
                              },
                              textAlign: TextAlign.center,
                              inputDecoration: const InputDecoration(
                                  border: InputBorder.none,
                                  contentPadding:
                                      EdgeInsets.symmetric(horizontal: 10),
                                  isCollapsed: true,
                                  isDense: true));
                        })
                      ])),
                  Obx(() {
                    return Text("最终模拟位置:${state.location.value}");
                  }),
                ]))
          ]))),
          Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(children: [
                Expanded(
                    child: ElevatedButton(
                        onPressed: logic.mock, child: const Text("启动模拟"))),
                Expanded(
                    child: ElevatedButton(
                        onPressed: logic.stopMock, child: const Text("停止模拟")))
              ]))
        ]));
  }
}
